﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Windows.Input;
using System.Windows;
using GUILib;
using System.Diagnostics;

namespace MetaTracker.ViewModel
{
    internal class RegisterFitbitViewModel : TrackerBaseViewModel, IRequestCloseViewModel
    {
        #region Data and Properties

        

        private string _URL = "http://www.xkcd.com/";
        public string URL
        {
            get
            {
                return _URL;
            }
            set
            {
                _URL = value;
                NotifyPropertyChanged("URL");
            }
        }

        private string _Verification = "";
        public string VerificationCode
        {
            get
            {
                return _Verification;
            }
            set
            {
                _Verification = value;
                NotifyPropertyChanged("VerificationCode");
            }
        }

        #endregion //Data and Properties

        #region Constructors
        public RegisterFitbitViewModel()
        {
        }
        #endregion //Constructors

        #region Commands

        public ICommand LaunchRegistration
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => LaunchRegistration,
                    (parameter) => DoLaunchRegistration(parameter),
                    () => true);
            }
        }

        private void DoLaunchRegistration(object parameter)
        {
            this.RequestToken();
            Process.Start(new ProcessStartInfo(this.URL));
        }
        
        public ICommand ValidateOauthCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => ValidateOauthCommand,
                    (parameter) => DoValidateOauthCommand(parameter) ,
                    CanValidate);
            }
        }
        private void DoValidateOauthCommand(object parameter)
        {
            if (tracker.RegisterFitbit_GetAccessToken(VerificationCode))
            {
                //close
                RequestClose(this, new DialogResultsEventArgs(true));
            }
            else
            {
                MessageBox.Show("The Validation Code appears to be incorrect, or expired.  Please try again.",
                    "Error Validating");
                VerificationCode = "";

            }
        }
        private bool CanValidate()
        {
            return !(string.IsNullOrEmpty(VerificationCode));
        }
        #endregion //COmmands

        #region Methods

        /// <summary>
        /// Generate a Request Token and its URL, and 
        /// </summary>
        public void RequestToken()
        {
            string results = null;

            for (int i = 0; i < 5 && results == null; i++)
            {
                results = tracker.RegisterFitbit_GetRequetURL();
            }

            URL = results;
        }

        #endregion //Methods



        public event EventHandler<GUILib.DialogResultsEventArgs> RequestClose;
    }
}
